package com.warehouse.module.business.controller.admin.cargoplan.convert;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;

/**
 * @author liutao
 * @version 1.0
 * @description: 地服 数据转换
 * @date 2024/8/21 13:51
 */
public class TheGroundConvert implements Converter<Integer> {
    @Override
    public Class<?> supportJavaTypeKey() {
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Integer convertToJavaData(ReadConverterContext<?> context) {
        String valueStr = context.getReadCellData().getStringValue();
        int value = 2;
        if (StrUtil.isNotEmpty(valueStr)) {
            if (StrUtil.equals(valueStr, "CA")) {
                value = 1;
            } else if (StrUtil.equals(valueStr, "BGS")) {
                value = 2;
            }
        }
        return value;
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Integer> context) {
        Integer value = context.getValue();
        String result = "BGS";
        if (ObjectUtil.isNotNull(value)) {
            if (value == 1) {
                result = "CA";
            } else {
                result = "BGS";
            }
        }
        return new WriteCellData<>(result);
    }

}
